////////////////////////////////////////////////////////////////////////////////
//
//  FLYING KIWI BV
//  Copyright 2010 Flying Kiwi BV
//  All Rights Reserved.
//
//  NOTICE: Flying Kiwi permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package plantool.display.windows {
	//--------------------------------------------------------------------------
	//  Imports
	//--------------------------------------------------------------------------
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import kiwi.events.CallbackEvent;
	
	import plantool.core.CoreDisplayObject;
	import plantool.display.windows.objects.ButtonRow;
	import plantool.display.windows.objects.WindowFooter;
	import plantool.display.windows.objects.WindowHeader;
	import plantool.display.windows.objects.WindowRow;
	import plantool.display.windows.objects.WindowRowContainer;
	import plantool.display.windows.objects.WindowRowSeparator;
	import plantool.events.WindowEvent;
	
	//--------------------------------------------------------------------------
	//  [Metadata (Events, Style, Effects etc)]
	//--------------------------------------------------------------------------
	
	//--------------------------------------------------------------------------
	//  Class declaration
	//--------------------------------------------------------------------------
	public class BaseWindow extends CoreDisplayObject {
		//--------------------------------------------------------------------------
		//Class constants
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Class variables
		//--------------------------------------------------------------------------
		private var _width:Number		= 300;
		private var _height:Number		= 0;

		private var _buttonRowSeperator:WindowRowSeparator	= null;
		private var _buttonRow:ButtonRow					= null;
		private var _header:WindowHeader					= null;
		private var _footer:WindowFooter					= null;
		private var _createButtonRow:Boolean				= true;
		private var _contentContainer:WindowRowContainer	= new WindowRowContainer();
		
		//--------------------------------------------------------------------------
		//Class properties
		//--------------------------------------------------------------------------
		public function set title(value:String):void 	{ _header.title = value; }
		public function get title():String				{ return _header.title; }
		
		public function get buttonRow():ButtonRow 		{ return _buttonRow; }
		
		override public function get height():Number 			{ return _height; }
		override public function set height(value:Number):void 	{ _height = value; invalidate(); }
		override public function get width():Number 			{ return _width; }
		override public function set width(value:Number):void 	{ _width = value; invalidate(); }
		
		public function get numRows():uint { return _contentContainer.numChildren; }
		
		//--------------------------------------------------------------------------
		//Constructor and Destructor
		//--------------------------------------------------------------------------
		public function BaseWindow(createButtonRow:Boolean = true) {
			//store if we need to create a button row
			_createButtonRow = createButtonRow;
			
			//call super
			super();
		}
		
		public override function dispose():void {
			//dispose header
			if (_header) {
				_header.dispose();
				_header = null;
			}
			
			//dispose buttonRowSeperator
			if (_buttonRowSeperator) {
				_buttonRowSeperator.dispose();
				_buttonRowSeperator = null;
			}
			
			//dispose buttonRow
			if (_buttonRow) {
				_buttonRow.dispose();
				_buttonRow = null;
			}
			
			//dispose footer
			if (_footer) {
				_footer.dispose();
				_footer = null;
			}
			
			//remove content container
			if (_contentContainer) {
				_contentContainer.removeAll();
				_contentContainer.dispose();
				_contentContainer = null;
			}
			
			//super dispose
			super.dispose();
		}
		
		//--------------------------------------------------------------------------
		//Class methods
		//--------------------------------------------------------------------------
		private function mouseHandler_btnLeft(e:CallbackEvent):void {
			leftBtnHandler(e);
		}
		
		private function mouseHandler_btnRight(e:CallbackEvent):void {
			rightBtnHandler(e);
		}
		
		private function positionFooter():void {
			//do nothing if the footer is not yet created
			if (!_footer || !_header || !_contentContainer) return;
			
			//set new height for window
			if (_createButtonRow && _buttonRowSeperator && _buttonRow) {
				//set height
				height = _contentContainer.height + _header.height + _footer.height + _buttonRowSeperator.height + _buttonRow.height;
				
				//position the buttonRow + seperator
				_buttonRowSeperator.y 	= height - _buttonRowSeperator.height - _buttonRow.height - _footer.height;
				_buttonRow.y 			= height - _buttonRow.height - _footer.height;
			} else {
				height = _contentContainer.height + _header.height + _footer.height;
			}
			
			//position the footer
			_footer.y = height - _footer.height;
		}
		
		protected override function init():void {
			//create header
			_header 		= new WindowHeader(this);
			_header.width 	= width;
			_header.height	= 18;
			super.addChild(_header);

			//create container
			_contentContainer.y = 18;
			super.addChild(_contentContainer);
			
			//check if we need a button row
			if (_createButtonRow) {
				//add a separator
				_buttonRowSeperator = new WindowRowSeparator(this);
				super.addChild(_buttonRowSeperator);
				
				//add a button row
				_buttonRow 							= new ButtonRow(this);
				_buttonRow.buttonLeft.mouseHandler 	= mouseHandler_btnLeft;
				_buttonRow.buttonRight.mouseHandler = mouseHandler_btnRight;
				super.addChild(_buttonRow);
			}
			
			//create footer
			_footer 		= new WindowFooter(this);
			_footer.y		= 18;
			_footer.width 	= width;
			_footer.height	= 18;
			super.addChild(_footer);
			
			//set height
			positionFooter();
			
			//invalidate
			invalidate();
		}
		
		protected function leftBtnHandler(e:CallbackEvent):void {
			
		}
		
		protected function rightBtnHandler(e:CallbackEvent):void {
			
		}
		
		public override function addChild(child:DisplayObject):DisplayObject {
			//add child to contentContainer
			_contentContainer.addRow(child as WindowRow);
			
			//position the footer
			positionFooter();
			
			//position in center
			this.x = (base.stage.stageWidth - this.width) / 2;
			this.y = (base.stage.stageHeight - this.height) / 2;
			
			//return the added child
			return child;
		}
		
		public function clearContent():void {
			//remove all rows from the content
			_contentContainer.removeAll();
		}
		
		public override function show(onCompleteHandler:Function=null):void {
			//add window to stage
			base.stage.addChild(this);
			
			//set window topmost
			if (base.stage.numChildren > 0) {
				base.stage.setChildIndex(this, base.stage.numChildren -1);
			}
			
			//call super show
			super.show(onCompleteHandler);
		}
	}
}